#define SIM_NO_STATUS 0
#define SIM_OK 1
#define SIM_ERROR 2
#define SIM_CONNECT 3
#define SIM_CONNECT_FAIL 4
#define WAIT_IP 5
#define WAIT_PASV 6
#define SIM_NETWORK_OK 7  
#define SIM_CMGR 8 
#define SIM_CMTI 9
#define SIM_RING 10
#define SIM_BUSY 11
#define SIM_NO_ANSWER 12
#define SIM_CONNECT_OK 13
#define SIM_NO_CARRIER 14
#define SIM_NO_DIALTONE 15
#define SIM_WAIT_IP 16
#define SIM_WAIT_CONNECT 17
#define SIM_WAIT_FTP_CODE 18
#define SIM_SEND_OK 19 
#define SIM_FTP_RECEIVE 20 
#define SIM_WAIT_FTP_PASV 21 
#define SIM_WAIT_FTP_DATA 22
#define SIM_CLOSE_OK 23 
#define SIM_WAIT_NETWORK 24
#define SIM_WAIT_FTP_OK 25
#define SIM_SHUT_OK 26
#define SIM_WAIT_CIPMUX 27
#define SIM_CIPMUXZERO 28
#define SIM_CIPMUXONE 29
#define SIM_WAIT_CIPSTATUS 30
#define SIM_CIP_IPSTART 31
#define SIM_CIP_IPINITIAL 32
#define SIM_UNKWOWN 33
#define SIZE_PARAMETER 30
#define SIZE_COMMAND 20


/*
Wait until a change of sim_status
    return the new status
*/
int wait4status();    


/***********************************************
************** PHONE FUNCTIONS *****************
***********************************************/

/* 
Send SMS message
    number = the addressee
    text = the message
    return 0 if success, else 1
*/
int send_text(char* number, char* text);
int send_textf(flash char* number, flash char* text);

/*
Read SMS message
    location : address in SIM card memory
    return 0 if success, else 1
*/
int read_text(char* location);
int read_textf(flash char* location);

/* 
Make a call
    number : the phone number
    return 0 if success, else 1
*/
int make_a_call(char* number);
int make_a_callf(flash char* number);

/* 
Delete all text messages in the SIM card memory
    return 0 if success, else 1
*/
int delete_all_messages();

/* 
Send file to FTP
    server : domaine name of the FTP - for instance ftp.google.co.nz
    login : login of a FTP user
    password : password of a FTP user
    file : name of the file on the USB disk
*/
int send_data_to_ftp(unsigned char* server, unsigned char* port,unsigned char* login, unsigned char* password, unsigned char* file);


/* 
Get back file from FTP
    server : domaine name of the FTP - for instance ftp.google.co.nz
    login : login of a FTP user
    password : password of a FTP user
    file : name of the file
*/
int get_back_data_from_ftp(unsigned char* server, unsigned char* port,unsigned char* login, unsigned char* password, unsigned char* file);



/*************************************************
************ INTERPRET UART MESSAGES *************
*************************************************/

/*
Interpret messages received through UART
Function called when UART receive BUFFER_END_OF_FIELD
*/ 
void sim_interpret_message();

/* 
Save UART buffer in message
*/
void sim_scan(char* message);

/*
Interpret all commands which are not "standard" command
*/
void sim_interpret_command(char* command);
 
/**************************************************
************* BASIC C FUNCTIONS *******************
**************************************************/

/* 
Save command from i to the end in param
    ex : command = "aa bb cc dd ee ff gg"
    sim_get_end_from(command,param, 2) -> param = "bb cc dd ee ff gg"
    sim_get_end_from(command,param, 4) -> param = "cc dd ee ff gg"
*/
void sim_get_end_from(char* command, char* param, int i);

/*
Save the next word which begin at position i in command
    ex : command = "aa bb cc dd ee ff gg"
    sim_get_word_from(command, param, 0) -> param = "aa"
    sim_get_word_from(command, param, 2) -> param = "bb"
*/
int sim_get_word_from(char* command, char* param, int i);

/*
When PASV command sent to the dronecell, it received : 
    (213,186,33,207,81,35)
    Allows us to save 213.186.33.207 in param1
    and 81*256+35 in param2
    Examples : get_ip_port_from_pasv("(213,186,33,207,81,35)", param1, param2);
*/
void get_ip_port_from_pasv(char* param4, char* param1, char* param2);


/*
Delete all character "c" in message
*/
void delete_char(char* message, char c);

/**************************************************
*************** AT COMMANDS ***********************
**************************************************/

/*
    ALL THE AT COMMANDS FUNCTION RETURN 1 (= SIM_OK) IF IT IS OK, ELSE 0
*/


/*
Verify the network status
    
*/
int CGREG();

/* 
Select SMS Message Format
        0 : PDU mode
        1 : text mode
*/    
int CMGF(flash unsigned char* i);

/*
Select character set
    We use GSM : 7 bits default alphabet
*/
int CSCS();

/*
Read text message
    location : address of the text message in SIM card memory
*/
int CMGR(char* location);
int CMGRf(flash char* location);

/*
Clear one text message
    location : address of the text message in SIM card memory
*/
int CMGD(char* location);
int CMGDf(flash char* location);

/*
Call a mobile phone
    number : the mobile phone number
*/
int ATD(char* number);
int ATDf(flash char* number);

/*
SMS Service center address
    Change the value in sim.c, depends of the operator
    2degrees : +64220227672
    vodafone : +6421600600 
*/
int CSCA();

/*
Set text mode parameters
    AT+CSMP=17,167,0,240
    17 = SMS_SUBMIT
    167 = TP-Validity-period either in integer format
    0 = TP-Protocol-Identifier in integer format
    241 = SMS Data Coding Scheme in Integer Format 
*/
int CSMP();

/*
Begin to send SMS message to a number
Return a prompt.        
*/
int CMGS(unsigned char* number);
int CMGSf(flash unsigned char* number);

/*
Send a text to UART and finish by 26 (<CTRL+Z>)
*/
int send_message_prompt(unsigned char* text);
int send_message_promptf(flash unsigned char* text);

/*
Close TCP or UDP connection.
    CIPCLOSEf("0") close the first connection
    CIPCLOSEf("1") close the second connection
    CIPCLOSEf("") close the current connection (only if there is only ONE connection)
*/
int CIPCLOSEf(flash unsigned char* i);



/*
Begin to send data through TCP or UDP connection
    CIPSENDf("0") send data to the first connection
    CIPSENDf("1") send data to the second connection
    CIPSENDf("") send data to the current connection (only if there is only ONE connection)
*/
int CIPSENDf(flash unsigned char* i);

/*
Set the APN : GPRS access point name, depends of the operator
    2degrees : http://m.2degreesmobile.co.nz/
    vodafone : http://www.vodafone.net.nz/ (need to be verified) 
*/
int CSTTf(flash unsigned char* address);

/*
Add an IP Head at the beggining of a packaged received 
    0 = not add IP header
    1 = add IP header, the format is "+IPD,data length:"
*/
int CIPHEADf(flash unsigned char* i);

/*
Bring up Wireless connection with GPRS
Can take a long time
*/
int CIICR();

/*
Get local IP address
If the device has an IP : return the IP
*/
int CIFSR();

/*
Get CIPMUX status
return SIM_CIPMUXZERO or SIM_CIPMUXONE
*/
int CIPMUX();

/*
Start up multi-IP connection
    0 : single IP connection
    1 : multi IP connection
*/
int CIPMUXf(flash unsigned char* i);

/*
Start up TCP or UDP connection
    number : the number of the new connection
    mode : TCP or UDP
    server : domaine name - for instance : ftp.martintheault.fr
    port : 21 for FTP, 80 for HTTP or other...
*/
int CIPSTART(flash unsigned char* number, flash unsigned char* mode, unsigned char* server,  unsigned char* port);

/*
Deactivate GPRS PDP Context
*/
int CIPSHUT();

/*
Return the CIPSTATUS
*/
int CIPSTATUS();

/**************************************************
*************** FTP COMMANDS **********************
**************************************************/

/*
    ALL THE FTP COMMANDS RETURN 1 (= SIM_OK) IF IT IS OK, ELSE 0
*/

/*
Send a FTP command. Has to finish by \r\n
*/
int send_ftp_commandf(flash unsigned char* command);

/*
Change the directory on the FTP
    directory : the directory
*/
int CWD(flash unsigned char* directory);

/*
Send a file to the ftp
    file : the name of the file
*/
int STOR(char* file);

/*
Retrieve a file on a FTP
    file : the name of the file
*/
int RETR(char* file);

/*
Send the command USER login
    login : the login
*/
int FTP_USER(unsigned char* login);  

/*
Send the command PASS password
    password : the password
*/
int FTP_PASS(unsigned char* password);

/*
Active the passive mode on the FTP and connect the dronecell to the FTP with the new PORT
*/
int PASV();
